import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import store from '../store';
const routes: Array<any> = [
  {
    path: '/',
    name: 'index',
    meta: { keepAlive: true },
    component: () => import('../views/index/index.vue'),
    children: [
      {
        path: 'examAdd',
        name: 'examAdd',
        meta: { keepAlive: false },
        component: () => import('../components/examAdd.vue'),
      },
      {
        path: 'examAnalysis',
        name: 'examAnalysis',
        meta: { keepAlive: false },
        component: () => import('../views/test/examAnalysis.vue'),
      },
      {
        path: 'indexAdd',
        name: 'indexAdd',
        meta: { keepAlive: false },
        component: () => import('../views/subjects/indexAdd.vue'),
      },
      {
        path: 'set',
        name: 'set',
        meta: { keepAlive: false },
        component: () => import('../views/set/index.vue'),
      },
      {
        path: 'databasequestion',
        name: 'Databasequestion ',
        meta: { keepAlive: true },
        component: () => import('../views/databaselist/databasequestion.vue'),
      },
      {
        path: 'examcheck',
        name: 'checkcheck ',
        meta: { keepAlive: true },
        component: () => import('../views/exam/examcheck.vue'),
      },
      {
        path: 'examstu',
        name: 'examstu',
        meta: { keepAlive: true },
        component: () => import('../views/stutest/examstu.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    meta: { keepAlive: true },
    component: () => import('../views/login/index.vue'),
  },
  {
    path: '/StutestPass',
    name: 'StutestPass',
    meta: { keepAlive: true },
    component: () => import('../views/stutest/StutestPass.vue'),
  },
  {
    path: '/PassMeim',
    name: 'PassMeim',
    meta: { keepAlive: true },
    component: () => import('../views/stutest/PassMeim.vue'),
  },
  {
    path: '/studentAnswers',
    name: 'studentAnswers',
    meta: { keepAlive: true },
    component: () => import('../views/stutest/studentAnswers.vue'),
  },
  {
    path: '/:catchAll(.*)',
    component: () => import('../views/error/error.vue'),
  }
];
const router = createRouter({
  history: createWebHashHistory(), // hash 路由模式
  // history: createWebHistory(), // history 路由模式
  routes, // 路由规则
});
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  }else {
    if (sessionStorage.getItem('token')) {
      let length = router.getRoutes().filter((item) => item.path == to.path).length;
      let urls: any = JSON.parse(sessionStorage.getItem('urls') as any);
      if (sessionStorage.getItem('urls') &&to.path != '/login' &&length === 0) {
        const modules: any = import.meta.glob('../views/*/*.vue');
        urls.forEach((item: any) => {
          let routerItem: any = {
            path: item.url,
            name: item.url,
            meta: { keepAlive: true },
            component: modules['../views' + item.url + '/index.vue'],
          };
          router.addRoute('index', routerItem);
        });
        next({ ...to, replace: true });
      } else {
        next();
      }
    } else {
      next('/login');
    }
  }
});
export default router;
